<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm"  ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="sql脚本" prop="sqlScript">
      <el-input v-model="dataForm.sqlScript" placeholder="sql脚本"></el-input>
    </el-form-item>
    <el-form-item label="更新方式" prop="updateMode">
      <el-input v-model="dataForm.updateMode" placeholder="更新方式"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="删除标志，0-正常，1-删除" prop="deleteFlag">
      <el-input v-model="dataForm.deleteFlag" placeholder="删除标志，0-正常，1-删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          tagId: 0,
          sqlScript: '',
          updateMode: '',
          createTime: '',
          updateTime: '',
          deleteFlag: ''
        },
        dataRule: {
          sqlScript: [
            { required: true, message: 'sql脚本不能为空', trigger: 'blur' }
          ],
          updateMode: [
            { required: true, message: '更新方式不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          deleteFlag: [
            { required: true, message: '删除标志，0-正常，1-删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.tagId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.tagId) {
            this.$http({
              url: this.$http.adornUrl(`/tag/usertagtask/info/${this.dataForm.tagId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sqlScript = data.userTagTask.sqlScript
                this.dataForm.updateMode = data.userTagTask.updateMode
                this.dataForm.createTime = data.userTagTask.createTime
                this.dataForm.updateTime = data.userTagTask.updateTime
                this.dataForm.deleteFlag = data.userTagTask.deleteFlag
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tag/usertagtask/${!this.dataForm.tagId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'tagId': this.dataForm.tagId || undefined,
                'sqlScript': this.dataForm.sqlScript,
                'updateMode': this.dataForm.updateMode,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'deleteFlag': this.dataForm.deleteFlag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
